const questionService = require('./../services/user-question')
const jwt = require('jsonwebtoken')

module.exports = {
  /**
 * 回答问题
 */
  async addAnswer ( ctx ) {
    let result = {
      success: false,
      message: '',
      data: null,
      code: '',
      totalCount: ''
    }
    let textCon = ctx.request.body.textCon
    const resultData = await questionService.userAnswer(textCon)
    if (!resultData) {
      result.code = 20000
      ctx.body = result
    } else {
      result.success = true
      result.message = '添加成功'
      result.code = 20000
      ctx.body = result
    }
  },
  /**
   * 
   * @param {添加问题} ctx 
   */
  async addQuestions ( ctx ) {
    let result = {
      success: false,
      message: '',
      code: '',
      totalCount: ''
    }
    let formData = ctx.request.body.formdata
    let token = ctx.request.body.token
    console.log(token)
    let decoded = jwt.decode(token)
    console.log(decoded)
    const stu_name = decoded.data.name
    formData.stu_name = stu_name
    const resultData = await questionService.userQuestions(formData)
    console.log(resultData)
    if (!resultData) {
      result.code = 20000
      ctx.body = result
    } else {
      result.success = true
      result.message = '添加成功'
      result.code = 20000
      ctx.body = result
    }
  },


/**
 * 
 * @param {上下文对象} ctx 
 * @param {加载问答列表  所有}  空参
 * @param {精选问题}   jxAsk
 * @param {付费问题}   payAsk
 * @param {精选问题}   jxAsk
 * @param {最新问题}   newAsk
 * @param {待回答问题}     waitAsk
 * 
 */
  async qstList ( ctx ) {
    let result = {
      success: false,
      message: '',
      data: null,
      code: '',
      totalCount: ''
    }
    let page = ctx.request.body.page
    let sort = ctx.request.body.sort
    const resultData = await questionService.getList(page,sort)
    if (!resultData) {
      ctx.body = result
    } else {
      result.success = true
      result.message = '加载成功'
      result.code = 20000
      result.data = resultData.result
      result.totalCount = resultData.count
      ctx.body = result
    }
  },
  /**
   * 
   * @param {列表标题模糊查询} find 
   */
  async Search ( ctx ) {
    let result = {
      success: false,
      message: '',
      data: null,
      code: 20000,
    }
    const findCon = ctx.request.body.search
    const resultData = await questionService.search(findCon)
    if (!resultData) {
      ctx.body = result
    } else {
      result.success = true
      result.message = '加载成功'
      result.code = 20000
      result.data = resultData.result
      ctx.body = result
    }
  },
  /**
   * @param {列表详情} ctx 
   */
  async askDetil ( ctx ) {
    let result = {
      success: false,
      message: '',
      data: null,
      code: '',
      totalCount: ''
    }
    let id = ctx.request.body.id
    const resultData = await questionService.getdetil(id)
    if (!resultData) {
      result.code = 20000
      ctx.body = result
    } else {
      result.success = true
      result.message = '加载成功'
      result.code = 20000
      result.data = resultData.result
      result.totalCount = resultData.count
      ctx.body = result
    }
  },
  /**
   * 查询用户提问问题
   */
  async SerchQst ( ctx ) {
    let result = {
      success: false,
      message: '',
      data: null,
      code: 20000,
    }
    let name = ctx.request.body.name
    console.log(name)
    const resultData = await questionService.userQstTitle(name)
    if (!resultData) {
      ctx.body = result
    } else {
      result.success = true
      result.message = '加载成功'
      result.code = 20000
      result.data = resultData.result
      ctx.body = result
    }
  },

    /**
   * 查询用户发表
   */
  async UserAddQst ( ctx ) {
    let result = {
      success: false,
      message: '',
      data: null,
      code: 20000,
    }
    let name = ctx.request.body.name
    console.log(name)
    const resultData = await questionService.getQstTitle(name)
    if (!resultData) {
      ctx.body = result
    } else {
      result.success = true
      result.message = '加载成功'
      result.code = 20000
      result.data = resultData.result
      ctx.body = result
    }
  },
}